package com.hsck.order.kds.base.isnoam.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.hsck.order.kds.R

/**
 * @author isnoam
 * @package_name CustomDividerItemDecoration
 * @date 2024-10-08
 * @description：
 */
class CustomDividerItemDecoration(
    context: Context,
    private val divider: Drawable = ContextCompat.getDrawable(
        context,
        R.drawable.order_dynamics_dish_items_divider
    )!!,
) : RecyclerView.ItemDecoration() {

    override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val childCount = parent.childCount
        for (i in 0 until childCount - 1) { // 这里确保不绘制最后一个 child 的分割线
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams

            val top = child.bottom + params.bottomMargin
            val bottom = top + divider!!.intrinsicHeight
            divider.setBounds(parent.paddingLeft, top, parent.width - parent.paddingRight, bottom)
            divider.draw(canvas)
        }
    }
}
